VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "sgCodeTemplate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Attribute VB_Ext_KEY = "Member0" ,"CodeBlocks"
Option Explicit

Private mvarTemplateFile As String
Private mvarName As String
Private mvarCodeBlocks As CodeBlocks
Private mvarParameters As Parameters


Const skeySectOpen = "{{"
Const skeySectClose = "}}"
Const skeyCodeBlock = "{CodeBlock}"
Const skeyEndCode = "{EndCode}"



Public Property Get Blocks() As CodeBlocks
    Set Blocks = mvarCodeBlocks
End Property

Public Property Get TemplateFile() As String
Attribute TemplateFile.VB_Description = "Full template file path"
    TemplateFile = mvarTemplateFile
End Property

Public Property Let TemplateFile(ByVal vData As String)
    mvarTemplateFile = vData
    If (mvarName <> "") Then Update
End Property

Public Property Get Name() As String
    Name = mvarName
End Property

Public Property Let Name(Name As String)
    mvarName = Name
    If (mvarTemplateFile <> "") Then Update
End Property

Public Property Get Parameters() As Parameters
    If mvarParameters Is Nothing Then
        Set mvarParameters = New Parameters
    End If
    Set Parameters = mvarParameters
End Property


'----------------------------------------------------------------------
' Implementation
'----------------------------------------------------------------------
Private Sub Update()
    On Error GoTo Error_
    
    Set mvarCodeBlocks = Nothing
    Set mvarCodeBlocks = New CodeBlocks
    If (mvarTemplateFile = "") Or (mvarName = "") Then Exit Sub
    
    ' Read template file
    Dim sFileText$, nFileNum%
    nFileNum = FreeFile
    Open mvarTemplateFile For Input As nFileNum
    sFileText = Input(LOF(nFileNum), nFileNum)
    Close nFileNum
    
    ' Find section with specified name
    Dim sSection$, sSectName$, nSectStart&, nSectEnd&
    sSectName = skeySectOpen & mvarName & skeySectClose
    nSectStart = InStr(sFileText, sSectName)
    If (nSectStart = 0) Then Exit Sub
    nSectStart = nSectStart + Len(sSectName)
    sSection = Mid(sFileText, nSectStart)
    
    ' Find end of the section
    nSectEnd = InStr(sSection, skeySectOpen)
    If (nSectEnd > 0) Then
        sSection = Left(sSection, nSectEnd - 1)
    End If
    sSection = Str_Trim(sSection)
    
    ' Parse all code blocks
    Dim sCodeBlock$, nCodeBlockStart&, nCodeBlockEnd&
    nCodeBlockEnd = 1
    Do
        ' Extract code block string
        nCodeBlockStart = InStr(nCodeBlockEnd, sSection, skeyCodeBlock)
        If (nCodeBlockStart = 0) Then Exit Do
        nCodeBlockEnd = InStr(nCodeBlockStart, sSection, skeyEndCode)
        If (nCodeBlockEnd < nCodeBlockStart) Then Exit Do
        nCodeBlockEnd = nCodeBlockEnd + Len(skeyEndCode)
        sCodeBlock = Mid(sSection, nCodeBlockStart, nCodeBlockEnd - nCodeBlockStart)
        
        ' Create code block object
        Dim block As codeBlock
        Set block = New codeBlock
        block.Initialize sCodeBlock, mvarParameters
        mvarCodeBlocks.Add block, block.Name
    Loop
    
    Exit Sub
Error_:
    MsgBox Error$
End Sub

Private Sub Class_Initialize()
    Set mvarCodeBlocks = New CodeBlocks
    Set mvarParameters = Nothing
End Sub

Private Sub Class_Terminate()
    Set mvarCodeBlocks = Nothing
    Set mvarParameters = Nothing
End Sub
